package com.ljj.consumer.controller;

import com.ljj.consumer.client.UserClient;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @ClassName ConsumerController
 * @Description TODO
 * @Author Administrator
 * @Date 2022/3/17 15:36
 * @Version 1.0
 */

@RestController
@RequestMapping("/consumer")
//@DefaultProperties(defaultFallback = "deafultFallback")
public class ConsumerController {
//    @Autowired
//    RestTemplate restTemplate;
//    @Autowired
//    private DiscoveryClient discoveryClient;
/*
    @GetMapping("{id}")
    @HystrixCommand(fallbackMethod = "queryByIdFallback")
    public String queryById(@PathVariable Long id){
//        String url ="http://localhost:9091/user/get/"+id;
        String url ="http://user-service/user/get/"+id;
//        List<ServiceInstance> instances = discoveryClient.getInstances("user-service");
//        ServiceInstance serviceInstance = instances.get(0);
//        String url = "http://"+serviceInstance.getHost() + ":" + serviceInstance.getPort()+"/user/get/"+id;

        return restTemplate.getForObject(url, String.class);
    }


 */
    @Autowired
    private UserClient userClient;

    @GetMapping("/{id}")
    public String queryById(@PathVariable Long id){
        return userClient.queryById(id);
    }
//    public String queryByIdFallback(Long id){
//        return "对不起，网络太拥挤了！";
//    }
//    public String deafultFallback(){
//            return "默认提示：对不起，网络太拥挤了！";
//    }
}
